package com.example.study_mybatis;

import com.example.study_mybatis.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 做一个调用
        String resourceConfig = "mybatis-config.xml";
        // 读取这个配置文件
        InputStream in = Resources.getResourceAsStream(resourceConfig);
        // 创建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = sqlSessionFactory.openSession();
        User user = new User();
        user.setUsername("张三");
        user.setPassword("000000");
        user.setNickname("这是mybatis创建的对象");
        session.insert("User.add", user);
        session.commit();
    }

    public void destroy() {
    }
}